<?php 
namespace app\admin\controller;
use app\admin\model\UploadFiles;
/**
* 商品分类管理
*/
class News extends Base
{
	//新闻展示
	public function new_list(){
		$pro=model("TkNews");
		$dataobj=$pro
					->field("id,new_name,new_content,ifshow,new_pubtime,keyword,issuer,pimgs")
					->order('id desc')
					->paginate("4",false);
		//exit;
		if(count($dataobj)==0){
			$this->success("请先添加商品","Newcate/add_new_cates");
		}
		foreach ($dataobj as $key => $value) {
			$data[]=$value->toArray();
		}		
		foreach ($data as $key => $value) {
			$data[$key]['pimgs']=explode(",", $value['pimgs']);
		}
		if(!isset($_GET['page']) || $_GET['page']<1){
			$_GET['page']=1;
			$nowpage=$_GET['page'];

		}else{
			$nowpage=$_GET['page'];
		}
		$page=$dataobj->render();
		$list=(($_GET['page']-1)*4)+1;
		$this->assign("nowpage",$nowpage);
		$this->assign("list",$list);
		$this->assign("page",$page);
		$this->assign("data",$data);
		return $this->fetch();
	}

	//新闻添加
	public function add_new(){
		if(request()->isPost()){
			$files = request()->file('pimgs');
			$upload=model("UploadFiles");
			$pro=model("TkNews");
			$pimgs=$upload->uploadAll($files);
			if(!is_array($pimgs)){
				$inf=$upload->uploadAll($files);
				$this->error("$inf");
			}
			$_POST['new_pubtime']=date("Y-m-d H:i:s", time());
			$pimgs=implode(",",$pimgs);
			$_POST['pimgs']=$pimgs;
			$new_name=$_POST['new_name'];
			$total=count($pro->where("new_name='{$new_name}'")->select());
			if($total==0){
				$num=$pro->allowField(true)->save($_POST);
				if($num==1){
					$this->success("添加成功",'new_list','',1);
				}else{
					$this->error("请重新添加",'','',1);
				}
			}else{
					$this->error("标题已存在",'','',1);
			}
		}else{
			$pc=model("TkNewCates");
			$cateobj=$pc->where("pc_level=2")->select();
			if(count($cateobj)==0){
				$this->error("请先添加二级分类",'add_pro_cates','',1);
			}else{
				// $city=model("TkCity");
				foreach ($cateobj as $key => $value) {
					$cates[]=$value->toArray();
				}
			}	
			$this->assign("cates",$cates);
			return $this->fetch();
		}	
	}

	//新闻修改
	public function  edit_new(){
		$pro=model("TkNews");
		$id=input("param.id");
		$data=$pro->find($id)->toArray();
		// print_r($data);
		// exit;
		$pimgs=explode(",",$data['pimgs']);
		if(request()->isPost() && request()->isAjax()){
				// print_r($pimgs);
				$src=input("post.src");
				// echo $src;
				$key=array_search($src,$pimgs);
				// echo $key;
				array_splice($pimgs,$key,1);
				// print_r($pimgs);
				$pimgs=implode(",",$pimgs);
				// echo $pimgs;
				$up=[
					'pimgs'=>$pimgs
				];
				$pro->where("id=$id")->update($up);
				exit;
		}elseif (request()->isPost()) {
			$total = count(request()->file('pimgs'));
			if($total==0){
				$num=$pro->allowField(true)->save(input("param."),["id"=>$id]);
				if($num){
					$this->success("添加成功",'new_list','',1);
				}else{
					$this->error("请重新添加",'edit_list','',1);
				}
			}else{
				$files=request()->file('pimgs');
				$upload=model("UploadFiles");
				$pimgs=$upload->uploadAll($files);
				$inform=$pro->find($id)->toArray();
				// echo $inform['pimgs'];
				$pimgs[]=$inform['pimgs'];
				// print_r($pimgs);
				$pimgs=implode(",",$pimgs);
				$_POST['pimgs']=$pimgs;
				// print_r($_POST);
				$num=$pro->allowField(true)->save($_POST,["id"=>$id]);
				if($num){
					$this->success("添加成功",'new_list','',1);
				}else{
					$this->error("请重新添加",'edit_list','',1);
				}
			}
		}else{
			$pc=model("TkNewCates");
			$cateobj=$pc->where("pc_level=2")->select();
			foreach ($cateobj as $key => $value) {
				$cates[]=$value->toArray();
			}
			$this->assign("cates",$cates);		
			$this->assign("data",$data);		
			$this->assign("pimgs",$pimgs);		
			return $this->fetch();
		}
	}

	//新闻删除
	public function del_new(){
		$pc=model("TkNews");
		$id=input("param.id");
		$dataobj=$pc->select();
		foreach ($dataobj as $v) {
			$data[]=$v->toArray();
		}
		$page=input("param.page");
		// echo $page;
		// exit;
		$num=$pc->destroy($id);
		//echo $_SERVER["HTTP_REFERER"];
		if($num){ 
			if(count($data)%4==1 && count($data)>4){
				$page=$page-1;
				$this->msg("删除成功",'http://www.think.com/admin/news/new_list.html?page'.$page);
			}else{
				$this->msg("删除成功",'http://www.think.com/admin/news/new_list.html?page'.$page);	
			}
		}else{
			$this->msg("删除失败",'http://www.think.com/admin/news/new_list.html?page'.$page);
		}
	}

	//新闻上下架
	public function new_ifshow(){
		$ifshow=input("param.ifshow");
		$id=input("param.id");
		if($ifshow==1){
			$ifshow=0;
		}else{
			$ifshow=1;
		}
		$pc=model("TkNews");
		$data=[
			"ifshow"=>$ifshow,
		];
		$num=$pc->where("id=$id")->update($data);
		//echo $num;
		// echo $pc->getLastSql();
		//exit;
		if($num==1){
			$this->success("修改成功",'new_list','',1);
		}else{
			$this->error("请重新修改",'new_list','',1);
		}
	}
}
?>





